// pages/songListDetail/songListDetail.js
const app = getApp();
var util = require("../../utils/util");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: "", //歌手头像
    name:'',//歌单名字
    listData: "",
    id: "", //当前id
    pageNumber: 1, //当前页码
    pageFromBanner: true, //从banner点击进入
    isEnd: true, //是否全部歌曲
    pageCount: 1, //总共页数
    totalcount:''//总共歌曲数量
  },
 // 收藏歌曲
 love(e) {
  var that = this;
    var songCode = e.currentTarget.dataset.code;
    var token = wx.getStorageSync("token");
    var tempParam = {
      songCode: songCode,
    };
    if (token) {
      util.postToken("/wechat/song/favorites/add", tempParam, token, function (res) {
        console.log(res, "收藏");
      });
    }
    // songCode
    // '/wechat/song/favorites/add'
},
// 添加歌曲
add(e) {
  var that = this;
  var codes = e.currentTarget.dataset.code;
  // "fa15884d-0ebb-388e-b179-363a077e089f"
  var uid = "de3e141b-b407-3b6b-9d0e-e4afae197f9b";
  // var uid = wx.getStorageSync("uid");
  var token = wx.getStorageSync("token");
  // console.log("avatarUrl:", wx.getStorageSync("avatarUrl"));
  // var headeimg = wx.getStorageSync("avatarUrl");
  var tempParam = {
    codes: codes,
    uid: uid,
    headurl:
      "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJSdKNnvw3qibIHicelmFau15pIFjKYBVzicGxrSKZPORnnU9DME6Wul89jkk277MJ1FmLooib9msOrsQ/132",
  };
  if (token) {
    util.postToken("/wechat/playlist/song/add", tempParam, token, function (res) {
      console.log(res);
    });
  } else {
  }
},
 // 播放歌曲
 play(e) {
  var that = this;
  console.log(e.currentTarget.dataset.code);
  var token = wx.getStorageSync("token");
  var code = e.currentTarget.dataset.code;
  var tempParam = {
    code: code,
  };
  if (token) {
    util.postToken("/wechat/play", tempParam, token, function (res) {
      console.log(res.status_code, "播放");
      if (res.status_code==200) {
        Toast.success('成功');
      } else {
        
      }
    });
  }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData);
    console.log(options);
    let that = this;
    let params = {
      id: app.globalData.gedanData.id,
      ordertype: 1,
      pagernumber: that.data.pageNumber,
    };
    that.setData({
      id: app.globalData.gedanData.id,
    });
    if (options.from == "banner") {
      that.setData({
        pageFromBanner: true,
      });
    } else {
      that.setData({
        pageFromBanner: false,
      });
    }

    util.postRes("/mobile/topicssong?pagesize=20", params, function (res) {
      console.log(res);
      if (res.status_code == 200) {
        that.setData({
          imgurl: res.data.spreadimg,
          name: res.data.name,
          listData: res.data.songlist,
          pageNumber: res.data.pagenumber,
          pageCount: res.data.pagecount,
          totalcount: res.data.totalcount,
        });
        that.isEnd();
      }
    });
  },
  // 是否全部歌曲
  isEnd() {
    var that = this;
    if (
      that.data.pageNumber < that.data.pageCount &&
      that.data.pageCount >= 2
    ) {
      that.setData({
        isEnd: false,
      });
    } else {
      that.setData({
        isEnd: true,
      });
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */ onReachBottom: function () {
    console.log("到底");
    var that = this;
    if (
      that.data.pageNumber < that.data.pageCount &&
      that.data.pageCount >= 2
    ) {
      setTimeout(function () {
        var pageNumber = Number(that.data.pageNumber) + 1;
        var params = {
          id: that.data.id,
          pagernumber: pageNumber,
          ordertype: 1,
        };
        util.postRes("/mobile/topicssong?pagesize=20", params, function (res) {
          if (res.status_code == 200) {
            that.setData({
              // imgurl: res.data.spreadimg,
              listData: that.data.listData.concat(res.data.songlist),
              pageNumber: res.data.pagenumber,
              pageCount: res.data.pagecount,
            });
            that.isEnd();
          }
        });
      }, 1000);
      clearTimeout();
    } else {
      that.setData({
        isEnd: true,
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
